package com.comleader.operator.validate;

import static cn.hutool.core.date.DatePattern.PURE_DATETIME_FORMATTER;
import static cn.hutool.core.text.CharSequenceUtil.hasBlank;
import static cn.hutool.core.text.CharSequenceUtil.isBlank;
import static com.comleader.operator.common.constant.Constant.SMS_OTHER;
import static com.comleader.operator.common.constant.Constant.SUBPORT;
import static com.comleader.operator.validate.CommonValidator.isDateFormatException;
import static com.comleader.operator.validate.CommonValidator.isDictIncorrect;

import java.util.List;

import com.comleader.operator.common.constant.DictConstant;
import com.comleader.operator.pojo.entity.DictDataEntity;
import com.comleader.operator.pojo.entity.SmsPortEntity;
import com.comleader.operator.service.DictDataService;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;

/**
 * @Dscription 短信端口校验器
 * @Author 连新景
 * @Date 2024/08/05 11:05
 */
public class SmsPortValidator {

    public static DictDataService dictDataService = SpringUtil.getBean(DictDataService.class);

    /*
     * @Description 校验格式异常
     * @Auhtor 连新景
     * @Date 2024/08/05 11:08
     * @param entity 
     * @return boolean
     */
    public static boolean isFormatException(SmsPortEntity entity) {
        if (hasBlank(entity.getLineNum(), entity.getSmsPort(), entity.getType(), entity.getBasicTelecomName(), entity.getAccessProvince(),
            entity.getCompanyName(), entity.getCompanyUsci(), entity.getLegalPersonName(), entity.getLegalPersonIdType(), entity.getLegalPersonIdNumber(),
            entity.getAgentName(), entity.getAgentIdType(), entity.getAgentIdNumber(), entity.getAuthStartTime(), entity.getAuthExpireTime(),
            entity.getSmsSign(), entity.getBusinessType(), entity.getOperatorLocation(), entity.getEnterpriseLocation())) {
            return true;
        }
        if (isDateFormatException(entity.getAuthStartTime(), PURE_DATETIME_FORMATTER)
            || isDateFormatException(entity.getAuthExpireTime(), PURE_DATETIME_FORMATTER)) {
            return true;
        }
        if (SUBPORT.equals(entity.getType())) {
            return isBlank(entity.getMasterPort());
        }
        // 多种业务类型采用 & 连接
        List<String> businessTypeList = StrUtil.split(entity.getBusinessType(), "&");
        if (businessTypeList.contains(SMS_OTHER)) {
            return isBlank(entity.getSpecificUse());
        }
        // 校验字段的值是否在字典配置中已配置
        if (isDictIncorrect(DictConstant.BASIC_TELECOM_NAME, entity.getBasicTelecomName())) {
            return true;
        }
        // 校验业务类型是否都在字典中有配置
        List<DictDataEntity> dictDataList = dictDataService.listDictDataByCode(DictConstant.SMS_BUSINESS_TYPE);
        List<String> dictValueList = CollStreamUtil.toList(dictDataList, DictDataEntity::getDictValue);
        return !dictValueList.containsAll(businessTypeList);
    }

}
